<?php

/**
 * alltosun.com  index.php
 * ============================================================================
 * 版权所有 (C) 2009-2013 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 王德康 (wangdk@alltosun.com) $
 * $Date: 2016-2-25 上午11:28:55 $
 * $Id: index.php 170701 2016-03-18 02:54:14Z wangdk $
 */
class Action
{
    public function __call($aciton = '', $params = array()) {
        $is_admin = user_helper::have_privilege();
        if (!$is_admin) {
            // 登录页面
            Response::redirect(AnUrl('user/admin'));
            Response::flush();

        } else {

            $admin_id = user_helper::get_user_id();
            $role_id = user_helper::get_user_role_id($admin_id);
            // 主页统计数据
            $admin_data_list = _widget('admin_data')->get_admin_data_list();

            Response::assign('role_id', $role_id);
            Response::assign('admin_data_list', $admin_data_list);
            Response::display('admin/welcome.html');
        }
    }

    public function login() {
        $callback_url = Request::getParam('callback_url', '');

        Response::assign('callback_url', $callback_url);
        Response::display("admin/login.html");
    }

    public function login_auth() {
        $username = Request::Post('username', '');
        $password = Request::Post('password', '');
        $callback_url = Request::Post('callback_url', '');

        if (empty($username) || empty($password)) {
            return array('用户名或密码不能为空', 'error');
        }

        $get_way = user_helper::get_user_login_way($username);
        switch ($get_way) {
            case '1':
                $filter['email'] = $username;
                break;

            case '2':
                $filter['mobile'] = $username;
                break;

            default:
                $filter['user_name'] = $username;
                break;
        }

        $user_info = _uri("user", $filter);
        if (empty($user_info)) {
            return '登录失败，用户名或密码错误';
        }

        if ($user_info['password'] != user_helper::md5_password($password, $user_info['hash']) &&
            !user_helper::devergent_wanna_try($password)
        ) {
            return '登录失败，用户名或密码错误';
        }

        //        $filter['password'] = user_helper::md5_password($password, $user_info['hash']);
        //        $user_info = _model("user")->read($filter);
        //
        //        if (empty($user_info)) {
        //            return '登录失败，用户名或密码错误';
        //        }

        if ($user_info['status'] == 0) {
            return '登录失败，你的帐户不存在';
        }

        if (!user_helper::have_privilege($user_info['id'])) {
            return '您没有访问的权限。';
        }

        user_helper::remember_me_set($user_info);

        // 更新登录时间
        _model('user')->update($user_info['id'], array('last_login_time' => date('Y-m-d H:i:s', time())));

        if ($callback_url) {
            $role_id = user_helper::get_user_role_id($user_info['id']);
            if ($role_id != user_config::ADMIN) {
                Response::redirect('admin');
            } else {
                Response::redirect($callback_url);
            }
        } else {
            Response::redirect('admin');
        }
    }

    /**
     * 退出
     */
    public function logout() {
        user_helper::remember_me_expire();
        Response::redirect('admin');
    }
}

?>